package com.dkd.system.utils;

import com.github.hui.quick.plugin.qrcode.wrapper.QrCodeGenWrapper;
import com.github.hui.quick.plugin.qrcode.wrapper.QrCodeOptions;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;

public class QRCodeGenerator {

	public static void main(String[] args) throws Exception {
		String url ="http://localhost:8089/details/13";
		byte[] bytes = generateBlackAndWhiteQRCode(url);
	}

	// 修改生成方法，增加容错率
	public static byte[] generateBlackAndWhiteQRCode(String url) throws Exception {
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		BufferedImage image = QrCodeGenWrapper.of(url)
				.setW(200)
				.setH(200)
				.setPadding(15)
				.setErrorCorrection(ErrorCorrectionLevel.M)
				.setDrawPreColor(0xFF000000)  // 纯黑
				.setDrawBgColor(0xFFFFFFFF)   // 纯白
				.asBufferedImage();

		// 添加微信识别标记（可选）
//		Graphics2D g = image.createGraphics();
//		g.setColor(Color.BLACK);
//		g.fillRect(130, 130, 40, 40);  // 中心位置加小方块
//		g.dispose();

		ImageIO.write(image, "png", baos);
		return baos.toByteArray();
	}

	// 生成带有logo的二维码
	public static byte[] generateQRCodeWithLogo(String url, InputStream logoStream) throws Exception {
		// 1. 基础参数设置
		int qrSize = 300;      // 二维码尺寸
		int logoSize = 60;     // Logo尺寸
		int border = 2;        // Logo白边宽度

		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		BufferedImage logoImage = ImageIO.read(logoStream);

		// 2. 创建带白边的Logo
		BufferedImage borderedLogo = new BufferedImage(
				logoSize + border*2,
				logoSize + border*2,
				BufferedImage.TYPE_INT_ARGB
		);

		Graphics2D g = borderedLogo.createGraphics();
		g.setColor(Color.WHITE);
		g.fillRect(0, 0, borderedLogo.getWidth(), borderedLogo.getHeight());
		g.drawImage(
				logoImage.getScaledInstance(logoSize, logoSize, Image.SCALE_SMOOTH),
				border,  // 水平偏移补正
				border,  // 垂直偏移补正
				null
		);
		g.dispose();

		// 3. 计算绝对居中坐标（数学修正版）
		BufferedImage qrImage = QrCodeGenWrapper.of(url)
				.setW(qrSize)
				.setH(qrSize)
				.setErrorCorrection(ErrorCorrectionLevel.H)
				.asBufferedImage();

		// 精准定位公式：
		int x = (qrImage.getWidth() - borderedLogo.getWidth()) / 2;
		int y = (qrImage.getHeight() - borderedLogo.getHeight()) / 2;

		// 4. 绘制并验证位置
		Graphics2D qrGraphics = qrImage.createGraphics();
		qrGraphics.drawImage(borderedLogo, x, y, null);

		// 调试用：绘制中心十字线（生产环境需删除）
		qrGraphics.setColor(Color.RED);
		qrGraphics.drawLine(qrSize/2, qrSize/2-10, qrSize/2, qrSize/2+10); // 竖线
		qrGraphics.drawLine(qrSize/2-10, qrSize/2, qrSize/2+10, qrSize/2); // 横线
		qrGraphics.dispose();

		ImageIO.write(qrImage, "png", baos);
		return baos.toByteArray();
	}

	// 生成彩色二维码
	public static byte[] generateColorQRCode(String url) throws Exception {
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		BufferedImage image = QrCodeGenWrapper.of(url)
				.setDrawPreColor(Color.BLUE)
				.asBufferedImage();
		ImageIO.write(image, "png", baos);
		return baos.toByteArray();
	}

	// 生成背景图二维码
	public static byte[] generateBackgroundImageQRCode(String url, InputStream bgImgStream) throws Exception {
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		BufferedImage image = QrCodeGenWrapper.of(url)
				.setBgImg(bgImgStream)
				.setBgOpacity(0.7F)
				.asBufferedImage();
		ImageIO.write(image, "png", baos);
		return baos.toByteArray();
	}

	// 特殊形状二维码
	public static byte[] generateSpecialShapeQRCode(String url) throws Exception {
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		BufferedImage image = QrCodeGenWrapper.of(url)
				.setDrawEnableScale(true)
				.setDrawStyle(QrCodeOptions.DrawStyle.DIAMOND)
				.asBufferedImage();
		ImageIO.write(image, "png", baos);
		return baos.toByteArray();
	}

	// 图片填充二维码
	public static byte[] generateImageFilledQRCode(String url, InputStream imgStream) throws Exception {
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		BufferedImage image = QrCodeGenWrapper.of(url)
				.setErrorCorrection(ErrorCorrectionLevel.H)
				.setDrawStyle(QrCodeOptions.DrawStyle.IMAGE)
				.addImg(1, 1, imgStream)
				.asBufferedImage();
		ImageIO.write(image, "png", baos);
		return baos.toByteArray();
	}

	// 生成gif动图二维码
	public static byte[] generateGifQRCode(String url, InputStream bgImgStream) throws Exception {
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		BufferedImage image = QrCodeGenWrapper.of(url)
				.setW(500)
				.setH(500)
				.setBgImg(bgImgStream)
				.setBgOpacity(0.6f)
				.setPicType("gif")
				.asBufferedImage();
		ImageIO.write(image, "gif", baos);
		return baos.toByteArray();
	}
}
